package com.dpwgc.pan.controller;

import com.dpwgc.pan.model.Notepad;
import com.dpwgc.pan.service.NotepadService;
import com.dpwgc.pan.utils.DateUtil;
import com.dpwgc.pan.utils.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.UUID;

/**
 * 记事本
 */
@CrossOrigin
@Api("记事本")
@RestController
@RequestMapping(value = "/Notepad")
public class NotepadController {

    @Autowired
    NotepadService notepadService;

    @Autowired
    DateUtil dateUtil;

    /**
     * 插入记事本
     * @param userId
     * @param notepadTitle
     * @param notepadContent
     * @return
     */
    @ApiOperation("插入记事本")
    @RequestMapping(value = "/insertNotepad", method = RequestMethod.POST)
    public ResultUtil<Object> insertNotepad(@RequestParam("userId") int userId,
                                            @RequestParam("notepadTitle") String notepadTitle,
                                            @RequestParam("notepadContent") String notepadContent){

        ResultUtil<Object> resultUtil = new ResultUtil<>();

        Notepad notepad = new Notepad();
        notepad.setUserId(userId);
        notepad.setNotepadTitle(notepadTitle);
        notepad.setNotepadContent(notepadContent);
        notepad.setCreateTime(dateUtil.getDateTime());
        notepad.setUpdateTime(dateUtil.getDateTime());
        notepad.setNotepadUuid(UUID.randomUUID().toString().replaceAll("-", ""));

        return notepadService.insertNotepadService(notepad);

    }

    /**
     * 更新记事本
     * @param notepadUuid
     * @return
     */
    @ApiOperation("更新记事本")
    @RequestMapping(value = "/updateNotepadByUuid", method = RequestMethod.POST)
    public ResultUtil<Object> updateNotepadByUuid(@RequestParam("notepadUuid") String notepadUuid,
                                                  @RequestParam("notepadTitle") String notepadTitle,
                                                  @RequestParam("notepadContent") String notepadContent){

        ResultUtil<Object> resultUtil = new ResultUtil<>();

        Notepad notepad = new Notepad();
        notepad.setNotepadTitle(notepadTitle);
        notepad.setNotepadContent(notepadContent);
        notepad.setUpdateTime(dateUtil.getDateTime());
        notepad.setNotepadUuid(notepadUuid);

        return notepadService.updateNotepadByUuidService(notepad);

    }

    /**
     * 删除记事本
     * @param notepadUuid
     * @return
     */
    @ApiOperation("删除记事本")
    @RequestMapping(value = "/deleteNotepadByUuid", method = RequestMethod.POST)
    public ResultUtil<Object> deleteNotepadByUuid(String notepadUuid){

        ResultUtil<Object> resultUtil = new ResultUtil<>();

        return notepadService.deleteNotepadByUuidService(notepadUuid);

    }

    /**
     * 获取该记事本详情
     * @param notepadUuid
     * @return
     */
    @ApiOperation("获取该记事本详情")
    @RequestMapping(value = "/getNotepadByUuid", method = RequestMethod.POST)
    public ResultUtil<Object> getNotepadByUuid(String notepadUuid){

        ResultUtil<Object> resultUtil = new ResultUtil<>();

        return notepadService.getNotepadByUuidService(notepadUuid);

    }

    /**
     * 获取用户记事本列表
     * @param userId
     * @return
     */
    @ApiOperation("获取用户记事本列表")
    @RequestMapping(value = "/listNotepadByUserId", method = RequestMethod.POST)
    public ResultUtil<Object> listNotepadByUserId(int userId){

        ResultUtil<Object> resultUtil = new ResultUtil<>();

        return notepadService.listNotepadByUserIdService(userId);

    }
}
